/*
* $Id: EditorDrawer.cpp 151 2010-09-23 11:22:00Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "EditorDrawer.h"
#include "EditorItem.h"

CEditorDrawer::CEditorDrawer(IWnd* pParent)
: CDrawer(pParent), m_nPos(0), m_nHeight(0), m_pSelectItem(NULL){}
CEditorDrawer::~CEditorDrawer(){
	Clear();
}

void CEditorDrawer::Paint(CDC* dc, CRect rect){
	CDC cdc;
	cdc.CreateCompatibleDC(dc);
	CBitmap bitmapTable;
	bitmapTable.CreateCompatibleBitmap(dc, rect.Size().cx, rect.Size().cy);
	HGDIOBJ bitmapOld = cdc.SelectObject(bitmapTable);
	CBrush brush;
	brush.CreateSolidBrush(GetSysColor(COLOR_3DFACE));
	cdc.FillRect(CRect(CPoint(0, 0), rect.Size()), &brush);
	brush.DeleteObject();

	int pos = m_nPos;
	for(std::vector<CEditorItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++){
		(*it)->SetPosY(pos);
		pos += (*it)->GetSizeY();
		(*it)->Paint(&cdc);
	}

	dc->BitBlt(rect.left, rect.top, rect.Size().cx, rect.Size().cy, &cdc, 0, 0, SRCCOPY);
	cdc.SelectObject(bitmapOld);
	bitmapTable.DeleteObject();
	cdc.DeleteDC();
}

int CEditorDrawer::GetHeightTable(){
	return m_nHeight;
}

void CEditorDrawer::SetBeginPos(int nPos){
	m_nPos = nPos;
}

void CEditorDrawer::SetSizeCx(int nSize){
	m_nHeight = 0;
	for(std::vector<CEditorItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++){
		(*it)->SetSizeCx(nSize);
		m_nHeight += (*it)->GetSizeY();
	}
}

void CEditorDrawer::SelectItem(CPoint pos){
	if(m_pSelectItem && m_pSelectItem->GetPosY() < pos.y &&	pos.y < m_pSelectItem->GetPosY() + m_pSelectItem->GetSizeY())
		return;
	for(std::vector<CEditorItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++){
		if((*it)->GetPosY() < pos.y && pos.y < (*it)->GetPosY() + (*it)->GetSizeY()){
			if(m_pSelectItem)
				m_pSelectItem->Activate(false);
			m_pSelectItem = *it;
			(*it)->Activate(true);
			break;
		}
	}
}

CEditorItem* CEditorDrawer::GetSelectItem(){
	return m_pSelectItem;
}

void CEditorDrawer::AddItem(CEditorItem* item){
	m_aItems.push_back(item);
}
void CEditorDrawer::Clear(){
	for(std::vector<CEditorItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++)
		delete *it;
	m_aItems.clear();
	m_pSelectItem = NULL;
}

bool CEditorDrawer::ProcessKey(unsigned int nChar, byte stateSysKey){
	if(!m_pSelectItem || m_pSelectItem && !m_pSelectItem->ProcessKey(nChar, stateSysKey)){
		if(nChar == VK_TAB && !(stateSysKey & 0x4)){
			if(m_pSelectItem)
				m_pSelectItem->Activate(false);
			m_pSelectItem = NextItem(m_pSelectItem);
			if(m_pSelectItem)
				m_pSelectItem->Activate(true);
		}
		if(nChar == VK_TAB && (stateSysKey & 0x4)){
			if(m_pSelectItem)
				m_pSelectItem->Activate(false);
			m_pSelectItem = BackItem(m_pSelectItem);
			if(m_pSelectItem)
				m_pSelectItem->Activate(true);
		}
	}
	return true;
}

CEditorItem* CEditorDrawer::NextItem(CEditorItem* pSelectItem){
	bool bfind = false;
	for(std::vector<CEditorItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++){
		if((!pSelectItem || bfind) && (*it)->WillActive() && !(*it)->IsHide()){
			return *it;
		}
		if(*it == pSelectItem)
			bfind = true;
	}
	return NULL;
}

CEditorItem* CEditorDrawer::BackItem(CEditorItem* pSelectItem){
	bool bfind = false;
	for(std::vector<CEditorItem*>::reverse_iterator it = m_aItems.rbegin(); it != m_aItems.rend(); it++){
		if((!pSelectItem || bfind) && (*it)->WillActive()){
			return *it;
		}
		if(*it == pSelectItem)
			bfind = true;
	}
	return NULL;
}

std::tstring CEditorDrawer::GetValueItem(int nId){
	std::tstring strValue;
	for(std::vector<CEditorItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++){
		if((*it)->m_nId == nId){
			strValue = (*it)->GetValue();
		}
	}
	return strValue;
}

std::tstring CEditorDrawer::GetNameItem(int nId){
	std::tstring strValue;
	for(std::vector<CEditorItem*>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++){
		if((*it)->m_nId == nId){
			strValue = (*it)->m_strName;
		}
	}
	return strValue;
}

bool CEditorDrawer::IsValidDrawer(Drawer::typeDrawer nType){
	return nType == Drawer::editor;
}

bool CEditorDrawer::ProcessMouse(unsigned int nButton, CPoint pos, int param){
	if(m_pSelectItem)
		return m_pSelectItem->ProcessMouse(nButton, pos, param);
	return false;
}